﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TPRG
    {

        public TPRG()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_PROGDEF();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
            if (dwMaster == null)
            { 
                
                KEY = "";
                CONTROLS = "";
            }
            else
            { 
                KEY = dwMaster["CONTROLS"].ToString();
                CONTROLS = dwMaster["CONTROLS"].ToString();
            }
        }
        
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                CONTROLS = "";
                return;
            }
            dwData = TBMain.NewRow();
            ((VICC_PROGDEF)MV).SetUpNewRow(ref dwData);
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "PRSEQ", 1);
            dwData["PROG"] = PROG.PadRight(80);
            dwData["CONTROLS"] = CONTROLS;
            dwData["REMARK"] = REMARK.Trim().PadRight(100);
            dwData["ROWNID"] = LFD;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
            IsNew = false;
            CONTROLS = "";
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VICC_PROGDEF)MV).UpdateICC_PROGDEF(dwMaster, app.company, 0);
            base.SaveCommand(sender, e);
        }
        
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "PRSEQ", 1);
            dwData["ROWNID"] = LFD;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        } 
       
        public override void OnPROGChg(string fc)
        {
            if (fc.Length < 1)
                return;
            TBMain.Clear();
            dwData = TBMain.NewRow();
            dwData["PROG"] = fc.PadRight(80);
            SResult = ((VICC_PROGDEF)MV).QueryICC_PROGDEF(dwData, 2);
            MergeData(SResult, false);
            base.OnPROGChg(fc);
        }
        public override void OnCONTROLSChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (value.Length < 1)
                return;
            ((VICC_PROGDEF)MV).CONTROLS = value;
            base.OnCONTROLSChg(value);
        }
        public override void OnREMARKChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            ((VICC_PROGDEF)MV).REMARK = value;
            base.OnREMARKChg(value);
        }        
    }
}
